<!-- 导入 -->
<template>
  <div class="importBtn">
    <el-upload
      ref="upload"
      :action="customUrl"
      :show-file-list="false"
      :auto-upload="true"
      accept=".xls,.xlsx"
      :headers="{ 'Blade-Auth': token }"
      :on-success="importSuccess"
      :on-error="importError"
    >
      <el-button
        slot="trigger"
        size="small"
        type="primary"
        icon="el-icon-upload2"
        style="margin-left: 5px"
        >导入</el-button
      >
    </el-upload>
  </div>
</template>

<script>
import { getToken } from "@/util/auth";
export default {
  props: {
    customUrl: {
      type: String,
      default: "",
    },
  },
  data() {
    return {};
  },
  computed: {
    token() {
      return "bearer " + getToken();
    },
  },
  components: {},
  methods: {
    importSuccess(e) {
      if (e && e.success) {
        this.$message.success(`导入成功，导入失败${e.data}条。`);
        this.$emit("refresh");
      } else {
        this.$message.error(e && e.msg ? e.msg : "导入失败");
      }
    },
    importError(e) {
      this.$message.error(e && e.msg ? e.msg : "导入失败");
    },
  },
};
</script>

<style lang="scss" scoped>
.importBtn {
  display: inline-block;
}
</style>
